/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.webapp.controller.cp;

import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import vn.webapp.controller.BaseWeb;
import vn.webapp.dto.DataPage;
import vn.webapp.form.ClassValidationForm;
import vn.webapp.form.RoomValidationForm;
import vn.webapp.model.Classes;
import vn.webapp.model.Rooms;
import vn.webapp.service.RoomsService;
/**
 *
 * @author Tonytran
 */
@Controller("cpRooms")
@RequestMapping(value = {"/cp"})
public class RoomsController extends BaseWeb{
    
    @Autowired
    private RoomsService roomsService;
    
    private int nav = 5;
    /**
     * 
     * @param model
     * @return 
     */
    @RequestMapping(value = "/rooms" , method = RequestMethod.GET)
    public String classes(ModelMap model, HttpSession session,
    @RequestParam(value = "page", defaultValue = "0") int page){
        int pageSize = 10;        
        DataPage<Rooms> dataRooms = roomsService.filter(page, pageSize);
        //DataPage<Rooms> dataRooms = roomsService.list();
        model.put("sz_Response", session.getAttribute("response"));
        session.setAttribute("response", "");
        model.put("dataRooms", dataRooms);
        model.put("pageIndex", page);
        model.put("nav", this.nav);
        return "cp.rooms";
    }
    
    /**
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/roomdetail/{id}")
    public String viewDetail(ModelMap model, @PathVariable("id") String id) {
        Rooms room = roomsService.viewDetail(id);
        model.put("roomForm", new RoomValidationForm());
        model.put("dataRoom", room);
        model.put("nav", this.nav);
        return "cp.roomdetail";
    }
    

    @RequestMapping(value = "/saveroomdetail", method = RequestMethod.POST)
    public String processEditClass(@Valid @ModelAttribute("roomForm") RoomValidationForm roomForm,
            BindingResult result,HttpServletRequest request, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.roomdetail";
        }else
        {
            String RoomName = roomForm.getName();
            int Capacity = roomForm.getCapacity();
            int RoomId = roomForm.getId();
            Rooms o_Room = roomsService.checkExistsName(RoomName, RoomId);
            if(o_Room != null)
            {
                model.put("sz_Response", "The name "+RoomName+" is being used.");
                return "cp.roomdetail";
            }else{
                int i_UpdateRoom = roomsService.updateTheRoom(RoomName, RoomId, Capacity);
                session.setAttribute("response", "Edit successfully!");
                return "redirect:" + this.baseUrl + "/cp/rooms.html";
            }
        }
    }
    
    @RequestMapping(value = "/removeroom/{id}", method = RequestMethod.GET)
    public String processRemoveRoom(ModelMap model, @PathVariable("id") int id, HttpSession session) {
        int i_IsRemoved = roomsService.removeRoom(id);
        String sz_Response;
        if (i_IsRemoved != 0) {
            sz_Response = "Remove the room successfully !";
        } else {
            sz_Response = "Remove the room not successfully. Please retry";
        }
        session.setAttribute("response", sz_Response);
        return "redirect:" + this.baseUrl + "/cp/rooms.html";
    }
    
    @RequestMapping(value = "/addnewroom", method = RequestMethod.GET)
    public String addnewroom(Map model){
        model.put("nav", this.nav);
        model.put("roomFormAdd", new RoomValidationForm());
        return "cp.addnewroom";
    }
    
    @RequestMapping(value = "/saveroom", method = RequestMethod.POST)
    public String processAddNewRoom(@Valid @ModelAttribute("roomFormAdd") RoomValidationForm roomFormAdd,
            BindingResult result, Map model, HttpSession session) {
        
        // Add the saved validationForm to the model  
        model.put("nav", this.nav);
        if (result.hasErrors()) {
           return "cp.addnewroom";
        }else
        {
            int Capacity = roomFormAdd.getCapacity();
            int Id = roomFormAdd.getId();
            String Name = roomFormAdd.getName();
            Rooms o_Room = roomsService.checkExistsName(Name, Id);
            if(o_Room != null)
            {
                model.put("sz_Response", "The name "+Name+" is being used.");
                return "cp.addnewroom";
            }else
            {
                int i_InsertARoom = roomsService.saveARoom(Name, Capacity);
                session.setAttribute("response", "Add successfully!");
                return "redirect:" + this.baseUrl + "/cp/rooms.html";
            }
        }
    }
}
